/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aso.upna.rmi;

import aso.upna.playlist.GenericList;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author triki
 */
public class GetCanciones extends UnicastRemoteObject
                          implements GetCancionesInterface
{
    private String path;

    public GetCanciones(String path_base) throws RemoteException
    {
        super();
        path = new String(path_base);
    }

    public GenericList loggedUsers() throws RemoteException {
        int i;
        String aux [] = null;
        GenericList l = new GenericList();

        File f = new File(path);
        String lista[] = f.list();

        for(i=0;i<lista.length;i++)
        {           
            if(lista[i].endsWith(".bin"))
            {
                String file = new String(lista[i]);
                String s = file.substring(0, file.lastIndexOf("."));
                l.addElement(s);
            }
        }

        return l;
    }

    public GenericList getCancionesFrom(String user) throws RemoteException {
        FileInputStream fi = null;
        {
            ObjectInputStream oi = null;
            try
            {
                File f = new File(path + user + ".bin");
                if (!f.exists())
                {
                    return null;
                }

                fi = new FileInputStream(f);
                oi = new ObjectInputStream(fi);

                Object aux = oi.readObject();

                oi.close();
                fi.close();

                if(aux instanceof GenericList)
                    return (GenericList)aux;
                
                return null;
            }
            catch (ClassNotFoundException ex)
            {
                return null;
            }
            catch (IOException ex)
            {
                return null;
            }
        }
    }

}
